/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sheep.view.Component;

import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JPanel;

/**
 *
 * @author geek
 */

    public class OneSideRoundedPanel extends JPanel {
        private BufferedImage cache;

        public static enum OneSideRoundedPanelMode {WEST,CENTER,EAST, NORTH, SOUTH};
        private OneSideRoundedPanelMode mode;
        
        public OneSideRoundedPanel(JComponent component,OneSideRoundedPanelMode m ) {
            super(new BorderLayout());
            mode = m;
            setOpaque(false); 
            component.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
            add(component);
        }

        public OneSideRoundedPanel(JComponent component,OneSideRoundedPanelMode m, int top, int right, int bottom, int left) {
            this(component,m);
            setOpaque(false); 
            component.setBorder(BorderFactory.createEmptyBorder(top, right, bottom, left));
            add(component);
        }
        
        @Override
        protected void paintComponent(Graphics g) {
            if (cache == null || cache.getWidth() != getWidth() ||
                cache.getHeight() != getHeight()) {
                cache = new BufferedImage(getWidth(), getHeight(),
                    BufferedImage.TYPE_INT_ARGB);

                Graphics2D g2 = cache.createGraphics();
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                    RenderingHints.VALUE_ANTIALIAS_ON);
                RoundRectangle2D rect= new RoundRectangle2D.Double(
                    0, 0, getWidth(), getHeight(), 12, 12);

                if (mode == OneSideRoundedPanelMode.EAST){
               
                  rect = new RoundRectangle2D.Double(
                    -20, 0, getWidth()+20, getHeight(), 12, 12);

                } else if (mode==OneSideRoundedPanelMode.CENTER){
                  rect = new RoundRectangle2D.Double(
                    -20, 0, getWidth()+40, getHeight(), 12, 12);
                    
                }else if(mode ==OneSideRoundedPanelMode.WEST){
                  rect = new RoundRectangle2D.Double(
                    0, 0, getWidth()+20, getHeight(), 12, 12);
                }else if(mode ==OneSideRoundedPanelMode.NORTH){
                  rect = new RoundRectangle2D.Double(
                    0, 0, getWidth(), getHeight()+20, 12, 12); 
                }else if(mode ==OneSideRoundedPanelMode.SOUTH){
                  rect = new RoundRectangle2D.Double(
                    0, -20, getWidth(), getHeight()+20, 12, 12);  
                }else{
                  rect = new RoundRectangle2D.Double(
                    0, 0, getWidth(), getHeight(), 12, 12);          
                }
                
                g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
                g2.setColor(Color.WHITE);
                g2.fill(rect);
            }

            g.drawImage(cache, 0, 0, null);
        }
    }